﻿using AutoMapper;
using AutoMapper.Mappers;
using Core.Dtos;
using Core.Domain;
using Core.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Mappers
{
    public class LogListTypeConverter : ITypeConverter<QueryResponse<Log>, QueryResponse<LogDto>>
    {
        public QueryResponse<LogDto> Convert(ResolutionContext context)
        {
            QueryResponse<Log> source = (QueryResponse<Log>)context.SourceValue;
            QueryResponse<LogDto> dest = new QueryResponse<LogDto>();
            dest.PageNumber = source.PageNumber;
            dest.PageSize = source.PageCount;
            dest.TotalItemCount = source.TotalItemCount;            

            var configuration = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
            configuration.CreateMap<Log, LogDto>()
                .ForMember(x => x.Exception, opt => opt.Ignore());
            var mapper = new MappingEngine(configuration);

            dest.List = mapper.Map<IList<Log>, IList<LogDto>>(source.List);
            return dest;
        }
    }
}
